<?php
class Cities extends MY_Controller
{
	var $curl = 'cities';
	var $name = '';
	var $search = '';
	var $per_page = 0;
	var $language;
	var $submenu = "_menu";
			
	function __construct()
	{
		parent::__construct();
				
		$this->load->model(array('/public/geo_city_model', '/public/geo_region_model', '/public/geo_state_model', '/public/geo_country_model'));

		$this->validation->set_fields($this->geo_city_model->fields);

		$this->data['Title'] = T_('Города');

		$this->language = parent::getLang();	
		
		$this->name = 'Города';
	}
	
	function index()
	{
		$this->geo_city_model->search();

		$this->renderLayout();
	}

	function create()
	{		
		$this->data['Title'] = $this->name . ' / ' . T_('Create');
		
		$this->validation->id_countries = ($this->input->post('id_countries')) ? $this->input->post('id_countries') : 0;
		$this->validation->id_states 	= ($this->input->post('id_states')) ? $this->input->post('id_states') : 0;
		$this->validation->id_regions 	= $this->input->post('id_regions');
		
		if (count($_POST))
		{
			$rules = array('id_countries', 'id_states', 'id_regions', 'name', 'phone_code');
			$this->validation->set_rules($this->geo_city_model->getRules($rules));

			if ($this->validation->run() === TRUE)
			{
				$item['id_states'] 			= $this->validation->id_states;
				$item['id_regions'] 		= $this->validation->id_regions;
				$item['name'] 				= $this->validation->name;
				$item['phone_code'] 		= $this->validation->phone_code;
				
				if ($this->geo_city_model->insert($item))
				{
					redirect($this->curl . '/index');	
				}				
			}
		}

		$this->renderLayout();		
	}
	
	function edit($id = null)
	{		
		if (!empty($id))
		{
			$city 	 = $this->geo_city_model->get($id);
			$country = $this->geo_state_model->getCountryByIdState($city['id_states']);

			$this->data['Title'] = $city['name'] . ' / ' . T_('Edit');
			
			if (!count($city))
			{
				$this->data['view'] = '_elements/query_no_object';
				$this->load->view($this->data['layout'], $this->data);
	
				return false; 
			}
		
			if (!count($_POST))
			{
				$this->validation->id 				= $city['id'];
				$this->validation->id_countries 	= $country->id;
				$this->validation->id_states 		= $city['id_states']; 
				$this->validation->id_regions		= $city['id_regions'];
				$this->validation->name 			= $city['name'];
				$this->validation->phone_code 		= $city['phone_code'];
			}
			else
			{
				$rules = array('id_countries', 'id_states', 'id_regions', 'name', 'phone_code');
				$this->validation->set_rules($this->geo_city_model->getRules($rules));
	
				if ($this->validation->run() === TRUE)
				{
					$item['id'] 				= $this->validation->id;
					$item['id_states'] 			= $this->validation->id_states;
					$item['id_regions'] 		= $this->validation->id_regions;
					$item['name'] 				= $this->validation->name;					
					$item['phone_code'] 		= $this->validation->phone_code;					
					
					$this->geo_city_model->update($item);
					
					redirect($this->curl . '/index');				
				}
			}			
		}
		
		$this->renderLayout();
	}
		
	function deleteItems()
	{
		$data = $this->geo_city_model->delete($_REQUEST['items']);

		echo $data;
	}

	function delete($id)
	{
		$data = $this->geo_city_model->delete($id);

		redirect($this->curl . '/index');
	}

	function loadCitiesByRegionId($region_ID = null, $wrap_elem = '')
	{
		$this->layout = 'ajax';
		$this->data['region_ID'] = $region_ID;
		$this->data['wrap_elem']  = $wrap_elem;
		$onchange = '';
		
		if ($wrap_elem == 'place_cities_2')
		{
			//$onchange = 'onchange="loadHotels(this.value, \'place_hotels_2\')"';
		}
		else if ($wrap_elem == 'place_cities')
		{
			$onchange = 'onchange="loadHotels(this.value, \'place_hotels\')"';
		}
		$this->data['onchange']  = $onchange;
		$this->load->view('cities/ajax/select_cities', $this->data);
	}	

	function loadCitiesByStateId($id_states = null, $wrap_elem = '', $onchange = '')
	{
		$this->layout = 'ajax';
		$this->data['id_states'] = $id_states;
		$this->data['wrap_elem'] = $wrap_elem;

		if (!$onchange) 
		{
			$onchange = 'loadDistrictsOfCity(this.value)';
		}
		
		if ($wrap_elem)
		{
			$onchange = 'onchange="'.$onchange.'"';
		}
		
		$this->data['onchange']  = $onchange;
		$this->load->view('cities/ajax/select_cities', $this->data);
	}			
	
	function loadCitiesByCountryId($country_ID = null, $wrap_elem = '')
	{
		$this->layout = 'ajax';
		$this->data['country_ID'] = $country_ID;
		$this->data['wrap_elem']  = $wrap_elem;
		if ($wrap_elem == 'place_cities_2')
		{
			$onchange = 'onchange="loadHotels(this.value, \'place_hotels_2\')"';
		}
		else if ($wrap_elem == 'place_cities')
		{
			$onchange = 'onchange="loadHotels(this.value, \'place_hotels\')"';
		}
		$this->data['onchange']  = $onchange;
		$this->load->view('cities/ajax/select_cities', $this->data);
	}	
	
	function search_autocomplete()
	{
		echo $this->geo_city_model->search_autocomplete();
	}
}
?>